package com.txw.javaCore.server.mq;

import com.txw.javaCore.model.entity.LoginEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @PROJECT_NAME: JavaCoreB2
 * @DESCRIPTION:
 * @Author: 涂玄武
 * @DATE: 2021/1/19 10:05
 */
@Component
public class LoginPublisher {

    private static final Logger log = LoggerFactory.getLogger(LoginPublisher.class);

    //定义发送消息的组件
    @Autowired
    private ApplicationEventPublisher publisher;

    /**
     * 发送消息
     * @throws Exception
     */
    public void sendMsg() throws Exception{

        //构建登录成功后用户的实体信息
        LoginEvent event = new LoginEvent(this, "helloWorld", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), "127.0.0.1");

        //发送消息
        publisher.publishEvent(event);

        log.info("Spring 事件驱动模型 - 发送消息：{}",event);
    }

}
